#include<stdio.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdlib.h>
#include<utmp.h>

void show_info(struct utmp *utbufp);

int main(int argc, char* argv[]) {	
	int				utmpfd;
	struct utmp		current_record;
	int				reclen = sizeof(current_record);
	//1. open the file
	if((utmpfd = open(UTMP_FILE,O_RDONLY)) == -1) {		//call open failure return -1
		//open failure 
		//UTMP_FILE defined in usr/include/paths.h is "/var/run/utmp"
		perror(UTMP_FILE);
		exit(1);
	}

	//2. read the file and show
	while(read(utmpfd, &current_record, reclen) == reclen){	// return num of read if success
		show_info(&current_record);
	}

	//3. close the file
	close(utmpfd);

	return 0;
}

void show_info(struct utmp *utbufp) {
	printf("% -8.8s", utbufp->ut_name);	
	printf("\n");
}
